package com.duolaibei.business.view.goods.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.duolaibei.business.R;
import com.duolaibei.business.base.BaseFragment;
import com.duolaibei.business.utils.inject.Inject;
import com.duolaibei.business.utils.log.LogDebug;
import com.duolaibei.business.view.activity.MyCaptureActivity;
import com.duolaibei.business.view.goods.mode.BatchBarcodeBean;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class BarCodeFragment extends BaseFragment {

    @Inject(R.id.recycle_batch_barcode)
    private RecyclerView mRecyleBarcode;

    private BarcodeAdapter barcodeAdapter;
    private List<BatchBarcodeBean> list;
    private String goodsCode;
    @Inject(R.id.btn_create_qrcode)
    private TextView mTxtQrcode;
    private final int REQUEST_GOODS_CODE = 222;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.fragment_barcode);
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        initBarcodeList();
        setBarcodeAdapter();
    }

    @Override
    protected void onSetListener() {
        mTxtQrcode.setOnClickListener(getCurrentFragment());
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_create_qrcode) {
            //自动生成商品条码

            String radomCode = "DLB" + getRadomInt();

        }
    }

    /**
     * 获取8位数随机数
     *
     * @return
     */
    private String getRadomInt() {
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < 8; i++) {
            int a = (int) (Math.random() * 9) + 1;
            buffer.append(String.valueOf(a));
        }
        return buffer.toString();
    }


    private void setBarcodeAdapter() {
        barcodeAdapter = new BarcodeAdapter(getContext(), R.layout.item_batch_barcode, list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        mRecyleBarcode.setLayoutManager(layoutManager);
        Drawable drawable = ContextCompat.getDrawable(Objects.requireNonNull(getActivity()), R.drawable.shape_barcode_div);
        DividerItemDecoration decoration = new DividerItemDecoration(Objects.requireNonNull(getActivity()), DividerItemDecoration.VERTICAL);
        if (drawable != null) {
            decoration.setDrawable(drawable);
            mRecyleBarcode.addItemDecoration(decoration);
        }
        mRecyleBarcode.setAdapter(barcodeAdapter);
    }

    private void initBarcodeList() {
        list = new ArrayList<>();
        BatchBarcodeBean batchBarcodeBean = new BatchBarcodeBean();
        batchBarcodeBean.setFormats("100");
        batchBarcodeBean.setQrcodeStr("QROCDE463518794");
        batchBarcodeBean.setGoodsNum("12345");
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);
        list.add(batchBarcodeBean);

    }

    private class BarcodeAdapter extends CommonAdapter<BatchBarcodeBean> {

        public BarcodeAdapter(Context context, int layoutId, List<BatchBarcodeBean> datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(ViewHolder holder, BatchBarcodeBean batchBarcodeBean, int position) {
            holder.setText(R.id.txt_barcode, batchBarcodeBean.getFormats());
            holder.setText(R.id.edit_batch_barcode, batchBarcodeBean.getQrcodeStr());
            holder.getView(R.id.scan_goods_code).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getActivity(), MyCaptureActivity.class);
                    intent.putExtra("title", "扫一扫");
                    startActivityForResult(intent, REQUEST_GOODS_CODE);
                }
            });
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_GOODS_CODE && resultCode == getActivity().RESULT_OK && data != null) {
            //处理扫描结果（在界面上显示）
            Bundle bundle = data.getExtras();
            if (bundle == null) {
                return;
            }
            if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                goodsCode = bundle.getString(CodeUtils.RESULT_STRING);
            } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                Toast.makeText(getActivity(), "解析二维码失败", Toast.LENGTH_LONG).show();
            }
        }

    }
}
